/**
 * Created by udi on 2016/11/28.
 */


require(["jv", "vue", "pop", "mui"], function () {
  var Vue = require("vue");

  if (!location.map.token) {
    jv.setToken();
  }

  var skip = 0, take = 5;

  var OrderStatusEnum = jv.enumer({
    waitPay: "未付款",
    paid: "已付款",
    confirmed: "已接单",
    working: "服务中",
    completed: "服务完成",
    closed: "已关闭",
    cancelled: "已取消"
  });

  var vm = jv.vm = new Vue({
    el: '#app',
    data: {items: [], loading: true, wuyes: [], ayis: []},
    computed: {},
    methods: {
      getStatus: function (status) {
        return OrderStatusEnum.getData(status);
      },
      toWuye: function (orderId, ev) {
        var target = ev.target;
        var pop = function () {
          jv.pop({
            id: "pop_wuye",
            data: jv.vm.$data.wuyes,
            idKey: "id",
            nameKey: "name",
            changed: function (value) {
              jv.post("/wuye/toWuye", {orderId: orderId, userId: value.id}, function (res) {

                var order = vm.$data.items.filter(function (it) {
                  return it.id == orderId;
                })[0];
                order.wuyeName = value.name;

              });
            }
          });
        }

        if (!this.wuyes.length) {
          jv.post("/wuye/getOther", function (res) {
            jv.vm.$data.wuyes = res;

            pop();
          });
          return;
        }
        pop();
      },
      toAyi: function (orderId) {

        var pop = function () {

          jv.pop({
            id: "ayis",
            data: jv.vm.$data.ayis,
            idKey: "id",
            nameKey: "name",
            changed: function (value) {
              jv.post("/wuye/toAyi", {orderId: orderId, userId: value.id}, function () {
                vm.$data.items.filter(function (it) {
                  return it.id == orderId;
                }).wuyeName = value.name;
              });
            }
          });
        }

        var toAyiProc = function () {
          jv.post("/wuye/getAyis", {}, function (res) {
            if (!res || !res.length) {
              mui.alert("还没有阿姨信息，请添加","&nbsp;");
              return;
            }
            jv.vm.$data.ayis = res;
            pop();
          });
        }
        //如果有阿姨，则提示
        if (!this.ayis.length) {
          toAyiProc();
          return;
        }

        pop();
        // var btnArray = ['否', '是'];
        // mui.confirm('当前已有阿姨在处理，是否要改变？', '', btnArray, function (e) {
        //   if (e.index == 1) {
        //     toAyiProc();
        //   }
        // });
      },
      getTime: function (dt) {
        var now = new Date();
        dt = new Date(dt);
        if( now.getFullYear() == dt.getFullYear()){
          return dt.toString("MM-dd HH:mm");
        }
        return dt.toString("yyyy-MM-dd HH:mm");
      }
    }
  });


  var bindVue = function (data) {
    skip += data.length;
    data.splice(0, 0, jv.vm.$data.items.length);
    data.splice(1, 0, 0);
    jv.vm.$data.items.splice.apply(jv.vm.$data.items, data);
  }

  jv.scroll2Bottom(function () {
    jv.post("/wuye/order/list", {skip: skip, take: take}, bindVue);
  }, true);

  if (window.android && window.android.setTitle) {
    window.android.setTitle("阿姨列表");
    window.android.setExtName("#");
    window.android.setExtCallback("(function(){location=location.origin + '/wuye/ayi/add';})");
  }
});
